package com.yh.modules.data.controller;

import cn.hutool.core.collection.CollUtil;
import com.yh.common.constant.CheckEnums;
import com.yh.common.utils.PageUtils;
import com.yh.common.utils.R;
import com.yh.common.utils.ValidatorUtils;
import com.yh.modules.data.entity.JqGridEntity;
import com.yh.modules.data.entity.MachineCheckEntity;
import com.yh.modules.data.entity.MachineDataEntity;
import com.yh.modules.data.entity.MachineDataWorkCountQueryForm;
import com.yh.modules.data.service.MachineCheckService;
import com.yh.modules.data.service.MachineDataService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 机器检测数据管理
 */
@RestController
@RequestMapping("/machine/data")
public class MachineDataController {
	@Autowired
	private MachineDataService machineDataService;

	@Autowired
	private MachineCheckService machineCheckService;


	/**
	 * 保存
	 */
	@PostMapping("/save")
	public R save(@RequestBody MachineDataEntity machineData) {
		ValidatorUtils.validateEntity(machineData);

		machineData.setCreateTime(new Date());
		machineData.setChecked(CheckEnums.UNCHECKED.getCode());

		machineDataService.insert(machineData);

		return R.ok();
	}


	/**
	 * 数据库
	 */
	@RequestMapping("/list")
	@RequiresPermissions("data:machine:list")
	public R list(@RequestParam Map<String, Object> params) {
		PageUtils page = machineDataService.getMachineDataList(params);

		return R.ok().put("page", page);
	}

	/**
	 * 获取数据库页面参数
	 */
	@RequestMapping("/getColNamesAndModel/{machineId}")
	@RequiresPermissions("data:machine:list")
	public R getColNamesAndModel(@PathVariable(value = "machineId") Integer machineId) {
		JqGridEntity jqGridEntity = machineDataService.getColNamesAndModel(machineId);

		return R.ok().put("jqGridEntity", jqGridEntity);
	}

	/**
	 * 数据分析
	 */
	@RequestMapping("/analyze")
	@RequiresPermissions("data:machine:list")
	public R analyze(@RequestParam Map<String, Object> params) {
		Map<String, Object> map = machineDataService.getMachineDataAnalyze(params);
		List<Map<String, Object>> list = CollUtil.newArrayList(map);
		return R.ok().put("analyzeData", list);
	}

	/**
	 * 获取数据分析页面参数
	 */
	@RequestMapping("/analyze/getColNamesAndModel/{machineId}")
	@RequiresPermissions("data:machine:list")
	public R getAnalyzeColNamesAndModel(@PathVariable(value = "machineId") Integer machineId) {
		JqGridEntity jqGridEntity = machineDataService.getAnalyzeColNamesAndModel(machineId);

		return R.ok().put("jqGridEntity", jqGridEntity);
	}

	/**
	 * 首检/巡检
	 */
	@RequestMapping("/check")
	@RequiresPermissions("data:machine:list")
	public R check(@RequestParam Map<String, Object> params) {
		List<Map<String, Object>> machineDataCheckList = machineDataService.getMachineDataCheck(params);
		return R.ok().put("checkData", machineDataCheckList);
	}

	/**
	 * 获取机器检测类型列表
	 */
	@RequestMapping("/checkTypeList/{machineId}")
	@RequiresPermissions("data:machine:list")
	public R getCheckTypeList(@PathVariable("machineId") Integer machineId) {
		List<MachineCheckEntity> machineCheckList = machineCheckService.getMachineCheckListByMachineId(machineId);
		return R.ok().put("machineCheckTypeList", machineCheckList);
	}



	@PostMapping("/workCount")
	@RequiresPermissions("data:machine:list")
	public R getMachineWorkCount(@RequestBody MachineDataWorkCountQueryForm from) {
		ValidatorUtils.validateEntity(from);

		machineDataService.getMachineWorkCount(from);
		return R.ok();
	}

	/**
	 * 数据统计
	 */
	@RequestMapping("/statistics")
	@RequiresPermissions("data:machine:list")
	public R statistics(@RequestParam Map<String, Object> params) {
		Map<String, Object> map = machineDataService.getMachineDataStatistics(params);
		List<Map<String, Object>> list = CollUtil.newArrayList(map);
		return R.ok().put("statisticsData", list);
	}

	/**
	 * 获取数据统计页面参数
	 */
	@RequestMapping("/statistics/getColNamesAndModel/{machineId}")
	@RequiresPermissions("data:machine:list")
	public R getStatisticsColNamesAndModel(@PathVariable(value = "machineId") Integer machineId) {
		JqGridEntity jqGridEntity = machineDataService.getStatisticsColNamesAndModel(machineId);

		return R.ok().put("jqGridEntity", jqGridEntity);
	}

}
